package com.jzr.task.db1.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzr.common.util.U;
import com.jzr.task.db1.mapper.MySysJobLogMapper;
import com.jzr.task.db1.mapper.TSysJobMapper;
import com.jzr.task.db1.model.TSysJob;
import com.jzr.task.db1.model.TSysJobLog;
import com.jzr.task.db1.service.TSysJobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class TSysJobServiceImpl extends ServiceImpl<TSysJobMapper,TSysJob> implements TSysJobService {
    @Autowired
    private MySysJobLogMapper sysJobLogMapper;

    public List<Integer> getIdList(Wrapper wrapper) {
        return baseMapper.getIdList(wrapper);
    }

    public Integer getId(Wrapper wrapper) {
        List<Integer> list = getIdList(wrapper);
        return list == null || list.size() <= 0 ? null : list.get(0);
    }

    @Override
    public Integer save2(TSysJob record) {
        if(record == null){
            return 0;
        }
        if(U.isNotBlank(record.getJobName())) {
            Integer id = getId(new QueryWrapper<TSysJob>().eq(TSysJob.JobName_,record.getJobName()));
            record.setId(id);
        }
        if(record.getId() == null){
            return baseMapper.insert(record);
        }else{
            return baseMapper.updateById(record);
        }
    }

    @Override
    public void addErrLog(Integer jobId, String msg) {
        if(jobId == null){
            return;
        }
        TSysJob job = new TSysJob();
        job.setId(jobId);
        job.setHasError(true);
        this.save(job);

        if(U.isBlank(msg)){
            return;
        }
        TSysJobLog sysJobLog = new TSysJobLog();
        sysJobLog.setJobId(jobId);
        sysJobLog.setUpdateTime(LocalDateTime.now());
        sysJobLog.setJobLog(msg);

        List<Integer> idList = sysJobLogMapper.getIdList(new QueryWrapper<TSysJobLog>().eq(TSysJobLog.JobId_,jobId));
        if(idList == null){
            sysJobLogMapper.insert(sysJobLog);
        }else{
            sysJobLogMapper.updateById(sysJobLog);
        }
    }

    @Override
    public void cleanErr(Integer jobId) {
        TSysJob job = new TSysJob();
        job.setId(jobId);
        job.setHasError(false);
        baseMapper.updateById(job);
    }

    @Override
    public LocalDateTime getLastSuccessDate(Integer id) {
        QueryWrapper<TSysJob> qw = new QueryWrapper<>();
        qw.select("last_success_date");
        qw.eq("id",id);
        TSysJob sysJob =  baseMapper.selectOne(qw);
        return sysJob == null ? null : sysJob.getLastSuccessDate();
    }

    @Override
    public Integer setLastSuccessDate(Integer id) {
        TSysJob record = new TSysJob();
        record.setId(id);
        record.setLastSuccessDate(LocalDateTime.now());
        return baseMapper.updateById(record);
    }
}
